cmake_minimum_required(VERSION 3.24)

project(yolov5 LANGUAGES CXX CUDA)
add_definitions(-DAPI_EXPORTS)
set(CMAKE_CXX_STANDARD 17)

# include and link dirs of cuda and tensorrt, you need adapt them if yours are different
# cuda
include_directories("C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8/include/")
include_directories(${PROJECT_SOURCE_DIR}/src/)
include_directories(${PROJECT_SOURCE_DIR}/plugin/)

file(GLOB_RECURSE SRCS ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/src/*.cu)
file(GLOB_RECURSE PLUGIN_SRCS ${PROJECT_SOURCE_DIR}/plugin/*.cu)

add_library(myplugins SHARED ${PLUGIN_SRCS})
target_link_libraries(myplugins nvinfer cudart)

set(OpenCV_DIR D:/Tool/opencv/build/x64/vc15/lib)
set(OpenCV_INCLUDE_DIRS D:/Tool/opencv/build/include)

find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(yolov5_det yolov5_det.cpp ${SRCS} )
target_link_libraries(yolov5_det nvinfer)
target_link_libraries(yolov5_det cudart)
target_link_libraries(yolov5_det myplugins)
target_link_libraries(yolov5_det ${OpenCV_LIBS})
set_target_properties(yolov5_det PROPERTIES
        CUDA_SEPARABLE_COMPILATION ON)

add_executable(yolov5_cls yolov5_cls.cpp ${SRCS} )
target_link_libraries(yolov5_cls nvinfer)
target_link_libraries(yolov5_cls cudart)
target_link_libraries(yolov5_cls myplugins)
target_link_libraries(yolov5_cls ${OpenCV_LIBS})
set_target_properties(yolov5_cls PROPERTIES
        CUDA_SEPARABLE_COMPILATION ON)

add_executable(yolov5_seg yolov5_seg.cpp ${SRCS} )
target_link_libraries(yolov5_seg nvinfer)
target_link_libraries(yolov5_seg cudart)
target_link_libraries(yolov5_seg myplugins)
target_link_libraries(yolov5_seg ${OpenCV_LIBS})
set_target_properties(yolov5_seg PROPERTIES
        CUDA_SEPARABLE_COMPILATION ON)

# 编译前copy文件
#add_custom_command(
#        TARGET yolov5_det  PRE_BUILD
#        COMMAND ${CMAKE_COMMAND} -E copy
#        ${PROJECT_SOURCE_DIR}/yolov5s.wts
#        ${CMAKE_CURRENT_BINARY_DIR}/yolov5s.wts)
